package com.example.smart.adapter.home

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.lib.extend.gone
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.visi
import com.example.smart.R
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.data.QuestionData
import com.example.smart.databinding.ItemQuestionBinding
import androidx.core.graphics.drawable.toDrawable
import com.example.lib.extend.dip2px
import com.example.lib.extend.setMargin
import com.example.lib.extend.setWidthOrHeight
import com.example.lib.utils.Logger
import com.example.smart.base.adapter.BaseRecyclerAdapter

open class QuestionAdapter(context: Context) : BaseRecyclerAdapter<ItemQuestionBinding, QuestionData>(context) {
    var choose: Int = 1
    var width = 0

    private val chooseTextColor by lazy {
        context.getColor(com.example.lib.R.color.white)
    }

    private val normalTextColor by lazy {
        context.getColor(com.example.lib.R.color.black_01104970)
    }

    private val successTextColor by lazy {
        context.getColor(com.example.lib.R.color.green_9dd030)
    }

    private val successDrawable by lazy {
        BitmapFactory.decodeResource(context.resources, R.mipmap.icon_question_success2).toDrawable(context.resources)
            .also {
                it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
            }
    }

    private val errorTextColor by lazy {
        context.getColor(com.example.lib.R.color.red_ff395d)
    }

    private val success by lazy {
        BitmapFactory.decodeResource(context.resources, R.mipmap.icon_success).toDrawable(context.resources).also {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
        }
    }

    private val error by lazy {
        BitmapFactory.decodeResource(context.resources, R.mipmap.icon_error).toDrawable(context.resources).also {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
        }
    }

    private val normal by lazy {
        BitmapFactory.decodeResource(context.resources, R.mipmap.icon_round_white).toDrawable(context.resources).also {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemQuestionBinding> {
        return BaseViewHolder(ItemQuestionBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_question, parent, false)))
    }

    override fun onBindView(itemView: ItemQuestionBinding, bean: QuestionData, position: Int) {
        Logger.e("变动:$position == $choose")
        if (width == 0) {
            itemView.root.post {
                width = itemView.root.width
            }
        }
        if (bean.default) {
            itemView.root.gone()
        } else {
            itemView.root.visi()
        }
        if (position == choose) {
            itemView.root.setWidthOrHeight(height = 360)
            itemView.root.setMargin(top = context.dip2px(0f))
            itemView.ItemTitle.setTextColor(chooseTextColor)
            itemView.ItemMsg.setTextColor(chooseTextColor)
            itemView.root.setBackgroundResource(com.example.lib.R.drawable.shape_25_326afd_solid)
            itemView.ItemImage2.visi()
        } else {
            itemView.root.setWidthOrHeight(height = 306)
            itemView.root.setMargin(top = context.dip2px(20f))
            itemView.ItemTitle.setTextColor(normalTextColor)
            itemView.ItemMsg.setTextColor(normalTextColor)
            itemView.root.setBackgroundResource(com.example.lib.R.drawable.shape_25_white_solid)
            itemView.ItemImage2.gone()
        }

        itemView.ItemTitle.text = "第${position}题"
        itemView.ItemMsg.text = bean.msg
        if (bean.a.isEmpty()) {
            itemView.ItemText1.gone()
        } else {
            itemView.ItemText1.visi()
            itemView.ItemText1.text = bean.a
        }

        if (bean.b.isEmpty()) {
            itemView.ItemText2.gone()
        } else {
            itemView.ItemText2.visi()
            itemView.ItemText2.text = bean.b
        }

        if (bean.c.isEmpty()) {
            itemView.ItemText3.gone()
        } else {
            itemView.ItemText3.visi()
            itemView.ItemText3.text = bean.c
        }

        if (bean.d.isEmpty()) {
            itemView.ItemText4.gone()
        } else {
            itemView.ItemText4.visi()
            itemView.ItemText4.text = bean.d
        }

        when (bean.select) {
            "A" -> {
                if (bean.success == "A") {
                    itemView.ItemText1.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText1.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText1.setCompoundDrawables(error, null, null, null)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                    itemView.ItemText1.setTextColor(errorTextColor)
                }

                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                if (position == choose) {
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "B" -> {
                if (bean.success == "B") {
                    itemView.ItemText2.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText2.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText2.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText2.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "C" -> {
                if (bean.success == "C") {
                    itemView.ItemText3.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText3.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText3.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText3.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)

                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "D" -> {
                if (bean.success == "D") {
                    itemView.ItemText4.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText4.setTextColor(errorTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText4.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText4.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)

                if (position == choose) {
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            else -> {
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                itemView.ItemError.gone()
                itemView.ItemSuccess.gone()

                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }
        }

        if (bean.select.isNotEmpty()) {
            if ("A" == bean.success && bean.success != bean.select) {
                itemView.ItemText1.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("B" == bean.success && bean.success != bean.select) {
                itemView.ItemText2.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("C" == bean.success && bean.success != bean.select) {
                itemView.ItemText3.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("D" == bean.success && bean.success != bean.select) {
                itemView.ItemText4.setCompoundDrawables(successDrawable, null, null, null)
            }
        }

        itemView.ItemText1.setOnDoubleClickListener {
            if (bean.select.isEmpty()) {
                updateItem(position, bean.also {
                    it.select = "A"
                }, "update")
            }
        }
        itemView.ItemText2.setOnDoubleClickListener {
            if (bean.select.isEmpty()) {
                updateItem(position, bean.also {
                    it.select = "B"
                }, "update")
            }
        }

        itemView.ItemText3.setOnDoubleClickListener {
            if (bean.select.isEmpty()) {
                updateItem(position, bean.also {
                    it.select = "C"
                }, "update")
            }
        }

        itemView.ItemText4.setOnDoubleClickListener {
            if (bean.select.isEmpty()) {
                updateItem(position, bean.also {
                    it.select = "D"
                }, "update")
            }
        }
    }

    override fun onItemChangeBindView(itemView: ItemQuestionBinding,
        bean: QuestionData,
        position: Int,
        payloads: MutableList<Any>) {
        when (bean.select) {
            "A" -> {
                if (bean.success == "A") {
                    itemView.ItemText1.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText1.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText1.setCompoundDrawables(error, null, null, null)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                    itemView.ItemText1.setTextColor(errorTextColor)
                }

                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                if (position == choose) {
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "B" -> {
                if (bean.success == "B") {
                    itemView.ItemText2.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText2.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText2.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText2.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "C" -> {
                if (bean.success == "C") {
                    itemView.ItemText3.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText3.setTextColor(successTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText3.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText3.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)

                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            "D" -> {
                if (bean.success == "D") {
                    itemView.ItemText4.setCompoundDrawables(success, null, null, null)
                    itemView.ItemText4.setTextColor(errorTextColor)
                    itemView.ItemError.gone()
                    itemView.ItemSuccess.visi()
                } else {
                    itemView.ItemText4.setCompoundDrawables(error, null, null, null)
                    itemView.ItemText4.setTextColor(errorTextColor)
                    itemView.ItemError.visi()
                    itemView.ItemSuccess.gone()
                    itemView.ItemError.text = bean.success
                }
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)

                if (position == choose) {
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }

            else -> {
                itemView.ItemText1.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText2.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText3.setCompoundDrawables(normal, null, null, null)
                itemView.ItemText4.setCompoundDrawables(normal, null, null, null)
                itemView.ItemError.gone()
                itemView.ItemSuccess.gone()

                if (position == choose) {
                    itemView.ItemText1.setTextColor(chooseTextColor)
                    itemView.ItemText2.setTextColor(chooseTextColor)
                    itemView.ItemText3.setTextColor(chooseTextColor)
                    itemView.ItemText4.setTextColor(chooseTextColor)
                } else {
                    itemView.ItemText1.setTextColor(normalTextColor)
                    itemView.ItemText2.setTextColor(normalTextColor)
                    itemView.ItemText3.setTextColor(normalTextColor)
                    itemView.ItemText4.setTextColor(normalTextColor)
                }
            }
        }

        if (bean.select.isNotEmpty()) {
            if ("A" == bean.success && bean.success != bean.select) {
                itemView.ItemText1.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("B" == bean.success && bean.success != bean.select) {
                itemView.ItemText2.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("C" == bean.success && bean.success != bean.select) {
                itemView.ItemText3.setCompoundDrawables(successDrawable, null, null, null)
            }

            if ("D" == bean.success && bean.success != bean.select) {
                itemView.ItemText4.setCompoundDrawables(successDrawable, null, null, null)
            }
        }

    }


    fun next() {
        updateItem(choose)
        choose++
        updateItem(choose)
    }

}